package com.xb.loan.usercenter.user.user.service.impl;

import com.xb.loan.log.exception.BizException;
import com.xb.loan.usercenter.service.user.IUserBankCardService;
import com.xb.loan.usercenter.service.user.IUserIdentifyService;
import com.xb.loan.usercenter.service.user.bean.UserBankCardVO;
import com.xb.loan.usercenter.service.user.constant.UserConst;
import com.xb.loan.usercenter.service.user.exception.IUserServiceException;
import com.xb.loan.usercenter.user.user.dao.UserBankCardDao;
import com.xb.loan.usercenter.user.user.dao.bean.UserBankCard;
import com.xb.loan.util.cache.redis.comm.FormatOperation;
import com.xb.loan.util.common.ObjectOperation;
import com.xb.loan.util.log.DefaultLoggerFacotry;
import com.xb.loan.util.mybatis.MyTransactional;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("userBankCardService")
public class UserBankCardServiceImpl implements IUserBankCardService,IUserServiceException {
	
	Logger logger = DefaultLoggerFacotry.getLogger(UserBankCardServiceImpl.class);
	
	@Autowired
    UserBankCardDao userBankCardDao;
	
	@Autowired
    IUserIdentifyService userIdentifyService;
	
	@MyTransactional
	@Override
	public int bindUserBankCard(UserBankCardVO userBankCardVO)
			throws BizException {
		//根据userId查询真实姓名
		String realname = userIdentifyService.queryRealnameByUserId(userBankCardVO.getUserId());
		//封装数据
		UserBankCard bankCard = ObjectOperation.convertByClasses(userBankCardVO, UserBankCard.class);
		//生成序列号
		bankCard.setBankcardId(FormatOperation.assemblySeq(UserConst.UserSeq.COMMONUSER.getName(), UserConst.UserSeq.COMMONUSER.getCode()));
		
        if (realname!=null || !"".equals(realname)) {
            bankCard.setCardUsername(realname);
        }
        //删除旧绑定的银行卡
        userBankCardDao.deleteByUserId(bankCard.getUserId());
        //绑定新银行卡
        return userBankCardDao.insert(bankCard);
	}

	@Override
	public UserBankCardVO queryDefaultUserBankCard(String userId)
			throws BizException {
		//根据userId查询银行卡信息
		UserBankCard userBankCard = userBankCardDao.selectBankCardByUserId(userId);
		//封装数据
		UserBankCardVO userBankCardVO = ObjectOperation.convertByClasses(userBankCard, UserBankCardVO.class);
		
		return userBankCardVO;
	}

	@Override
	public void updateCardUserNameByUserId(String userId, String cardUserName) {
		UserBankCard userBankCard = new UserBankCard();
		userBankCard.setUserId(userId);
		userBankCard.setCardUsername(cardUserName);
		//根据userId更新银行卡表中的cardUsername
		userBankCardDao.updateCardUserNameByUserId(userBankCard);
	}

	@Override
	public int deleteByPrimaryKey(String bankcardId) {
		
		return userBankCardDao.deleteByPrimaryKey(bankcardId);
	}

	@Override
	public int deleteByUserId(String userId) {
		
		return userBankCardDao.deleteByUserId(userId);
	}

	@Override
	public int insert(UserBankCardVO record) {
		
		UserBankCard ubc = ObjectOperation.convertByClasses(record, UserBankCard.class);
		return userBankCardDao.insert(ubc);
	}

	@Override
	public int insertSelective(UserBankCardVO record) {
		
		UserBankCard ubc = ObjectOperation.convertByClasses(record, UserBankCard.class);
		return userBankCardDao.insertSelective(ubc);
	}

	@Override
	public UserBankCardVO selectByPrimaryKey(String bankcardId) {

		UserBankCard ubc = userBankCardDao.selectByPrimaryKey(bankcardId);
		return ObjectOperation.convertByClasses(ubc, UserBankCardVO.class);
	}

	@Override
	public int updateByPrimaryKeySelective(UserBankCardVO record) {

		UserBankCard ubc = ObjectOperation.convertByClasses(record, UserBankCard.class);
		return userBankCardDao.updateByPrimaryKeySelective(ubc);
	}

	@Override
	public int updateByPrimaryKey(UserBankCardVO record) {

		UserBankCard ubc = ObjectOperation.convertByClasses(record, UserBankCard.class);
		return userBankCardDao.updateByPrimaryKey(ubc);
	}

	@Override
	public UserBankCardVO selectBankCardByUserId(String userId) {

		UserBankCard ubc = userBankCardDao.selectBankCardByUserId(userId);
		return ObjectOperation.convertByClasses(ubc, UserBankCardVO.class);
	}

	@Override
	public void updateCardUserNameByUserId(UserBankCardVO userBankCard) {

		UserBankCard ubc = ObjectOperation.convertByClasses(userBankCard, UserBankCard.class);
		userBankCardDao.updateCardUserNameByUserId(ubc);		
	}
	
}
